package practise.leetcode.q206;

class Solution {
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }

        if (head.next == null) {
            return head;
        }

        if (head.next.next  == null) {
            ListNode n2 = head.next;
            n2.next = head;
            head.next = null;
            return n2;
        }

        ListNode n1 = head;
        ListNode n2 = head.next;
        ListNode n3 = head.next.next;

        while (n3 != null) {
            ListNode wrapNode = n3.next;

            n2.next = n1;
            n3.next = n2;
            n1 = n2;
            n2 = n3;
            n3 = wrapNode;
        }

        head.next = null;
        return n2;
    }
}
